import md5 from 'blueimp-md5'
import hash from 'hash.js'

// 加密
export default function encrypt(params = {}) {
  const apiKey = '87c89300a0221b1299b6bb43ebe7daf9'
  // const time = Date.now()
  const keys = Object.keys(params).sort()
  // console.log('加密：', params)
  const sign = keys
    .reduce((collect, key) => {
      let item = params[key]
      if (['number', 'string'].includes(typeof item)) {
        item = (item + '').trim()
      }
      collect.push(item)
      return collect
    }, [])
    .join('')
  return hash
    .sha256()
    .update(`${md5(encodeURIComponent(sign))}${apiKey}`)
    .digest('hex')
}
